#ifndef _POLLER_
#define _POLLER_

#include "noncopyable.h"
#include "Timestamp.h"

#include <unordered_map>
#include <vector>

class Channel;
class EventLoop;

/*
    muduo庫多路事件分發器
*/
class Poller : noncopyable{
public:
    using ChannelList = std::vector<Channel*>;
    Poller(EventLoop *loop);
    virtual ~Poller();
    //保留統一給所有IO複用的接口
    virtual Timestamp poll(int timeoutMs, ChannelList *activeChannels) = 0;
    virtual void updateChannel(Channel *channel) = 0;
    virtual void removeChannel(Channel *channel) = 0;

    bool hasChannel(Channel *channel) const;
    //類似單例的getInstance()
    //爲了避免在基類中包含派生類的頭文件（這樣代碼太髒了）
    static Poller* newDefaultPoller(EventLoop *loop);
protected:
    //key表示監聽的sockfd，value表示這個socket屬於Channel
    using ChannelMap = std::unordered_map<int, Channel*>;
    ChannelMap channels_;
private:
    EventLoop *ownerLoop_;
};
#endif